//
//  DemoViewController.swift
//  UIViewDemo
//
//  Created by 劢克科技 on 2019/3/7.
//  Copyright © 2019 劢克科技. All rights reserved.
//

import UIKit

class DemoViewController: UIViewController {
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        
        // 手势
        let panGuesture = UIPanGestureRecognizer(target: self, action: #selector(interactiveGuestureAction(sender:)))
        view.addGestureRecognizer(panGuesture)
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissController))
        view.addGestureRecognizer(tapGesture)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.orange
    }
    
    @objc func interactiveGuestureAction(sender: UIPanGestureRecognizer) {
        if sender.state == .began {
            guard let transitionDelegate = self.transitioningDelegate as? PopAnimateDelegate else { return }
            transitionDelegate.gestureRecognizer = sender
            dismiss(animated: true, completion: nil)
        }
    }
    
    @objc func dismissController() {
        guard let transitionDelegate = self.transitioningDelegate as? PopAnimateDelegate else { return }
        transitionDelegate.gestureRecognizer = nil
        dismiss(animated: true, completion: nil)
    }
}

